<template>
  <div>
    <el-form inline :model="queryParticipant" ref="ParticipantQueryFormRef">
      <el-form-item>
        <el-input placeholder="关键字" v-model="queryParticipant.keywords"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParticipant.status" placeholder="状态" style="width: 240px">
          <el-option label="全部" :value="-1"/>
          <el-option label="正常" :value="0"/>
          <el-option label="禁用" :value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :icon="Search" @click="EventParticipantPage">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button :icon="Refresh" @click="ParticipantQueryFormRef?.resetFields()"
        >重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-button :icon="Plus" type="primary" @click="dialogVisible = true" size="small"
    >新增
    </el-button>
    <el-table :data="participantPage.list" border v-loading="loading">
      <el-table-column prop="participantId" label="参与记录ID"/>
      <el-table-column prop="eventId" label="活动编号"/>
      <el-table-column prop="userId" label="参与者"/>
      <el-table-column label="状态">
        <template #default="scope">
          {{ scope.row.status === 0 ? '新建' : '参与中' }}
        </template>
      </el-table-column>
      <el-table-column prop="appliedTime" label="申请时间">
        <template #default="scope">
          {{ scope.row.appliedTime ? new Date(scope.row.appliedTime).toLocaleString() : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="reviewedTime" label="审核时间" width="180">
        <template #default="scope">
          {{ scope.row.reviewedTime ? new Date(scope.row.reviewedTime).toLocaleString() : '' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button :icon="EditPen" @click="openModify(scope.row.participantId)" size="small" type="warning">修改
          </el-button>
          <el-divider direction="vertical"/>
          <el-button :icon="Delete" @click="openDel(scope.row.participantId)" size="small" type="danger">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
          v-model:current-page="queryParticipant.pageNum"
          :page-sizes="[8, 16, 32, 64]"
          v-model:page-size="queryParticipant.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="participantPage.total"
      />
    </div>
    <el-dialog
        :title="isAdd ? '新增' : '修改'"
        v-model="dialogVisible"
        width="500"
        @close="ParticipantRef?.resetFields()"
    >
      <el-form :model="Participant" ref="ParticipantRef" :rules="rules" label-width="80" size="large">
        <el-form-item label="活动编号">
          <el-input v-model="Participant.eventId"/>
        </el-form-item>
        <el-form-item label="参与者">
          <el-input v-model="Participant.userId"/>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker v-model="Participant.appliedTime" type="datetime" placeholder="选择日期时间"/>
        </el-form-item>
        <el-form-item label="审核时间">
          <el-date-picker v-model="Participant.reviewedTime" type="datetime" placeholder="选择日期时间"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="Participant.status">
            <el-radio :value="0">新建</el-radio>
            <el-radio :value="1">参与</el-radio>
            <el-radio :value="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEventParticipant(ParticipantRef)">保存</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive, onMounted, watch} from "vue";
import {Search, Refresh, Delete, EditPen, Plus} from "@element-plus/icons-vue";
import type {FormInstance, FormRules} from "element-plus";
import {ElMessage, ElMessageBox} from "element-plus";
import {
  reqAddEventParticipant,
  reqGetEventParticipantPage,
  reqGetEventParticipantById,
  reqUpdateEventParticipant,
  reqDelEventParticipant,
} from "@/api/social/event";
import type {SocEventParticipants} from "@/api/social/event/type";

let queryParticipant = reactive({
  pageNum: 1,
  pageSize: 8,
  keywords: "",
  status: -1,
});

let Participant = reactive<SocEventParticipants>({
  participantId: "",
  eventId: "",
  userId: "",
  status: 0,
  appliedTime: null,
  reviewedTime: null
});

let rules = reactive<FormRules<SocEventParticipants>>({
  eventId: [
    {required: true, message: "活动编号不能为空", trigger: "blur"},
  ],
  userId: [
    {required: true, message: "参与者不能为空", trigger: "blur"},
  ],
  status: [
    {required: true, message: "状态不能为空", trigger: "blur"},
  ],
});
let dialogVisible = ref(false);
let loading = ref<boolean>(true);
let isAdd = ref(true);
let ParticipantRef = ref<FormInstance>();
let ParticipantQueryFormRef = ref<FormInstance>();
let participantPage = reactive({
  total: 0,
  list: [] as SocEventParticipants[],
  pageNum: 0,
});

let EventParticipantPage = () => {
  reqGetEventParticipantPage(queryParticipant).then((res) => {
    Object.assign(participantPage, res.data);
    loading.value = false;
  });
};
let openModify = (participantId: string) => {
  reqGetEventParticipantById(participantId).then((res) => {
    Object.assign(Participant, res.data);
    isAdd.value = false;
    dialogVisible.value = true;
  });
};

let submitEventParticipant = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (isAdd.value) {
        await reqAddEventParticipant(Participant);
      } else {
        await reqUpdateEventParticipant(Participant);
        isAdd.value = true;
      }
      await EventParticipantPage();
      dialogVisible.value = false;
    } else console.log("error submit!", fields);
  });
};

let openDel = (participantId: string) => {
  ElMessageBox.confirm("删除帖子后不能恢复，是否删除？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
      .then(() => {
        reqDelEventParticipant(participantId).then(() => {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          EventParticipantPage();
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除",
        });
      });
};

onMounted(() => {
  EventParticipantPage();
});
watch([() => queryParticipant.pageNum, () => queryParticipant.pageSize], () => {
  EventParticipantPage();
});
</script>
<style scoped>
.border {
  color: var(--el-text-color-regular);
  border: 1px solid var(--el-border-color);
  border-radius: 0;
}
</style>
